package com.glamour.hlcity.module.url.analysis.impl;

import java.util.ArrayList;
import java.util.List;

import com.glamour.hlcity.common.data.model.UrlNode;
import com.glamour.hlcity.module.spider.factory.BeanFactory;
import com.glamour.hlcity.module.url.analysis.UrlAnalysis;

public class URLNodeMarket {
	private UrlNode urlNode;
	private List<String> urlList;
	
	
	public URLNodeMarket() {
		super();	
	}
	public URLNodeMarket(UrlNode urlNode, List<String> urlList) {
		super();
		this.urlNode = urlNode;
		loadUrls(urlList);
	}
	
	public URLNodeMarket(List<String> urlList) {
		super();
		loadUrls(urlList);
	}


	public UrlNode getUrlNode() {
		return urlNode;
	}
	public void setUrlNode(UrlNode urlNode) {
		this.urlNode = urlNode;
	}
	public List<String> getUrlList() {
		return urlList;
	}
	public void setUrlList(List<String> urlList) {
		this.urlList = urlList;
	}
	
	public void addUrl(String url){
		if(urlList==null){
			urlList=new ArrayList<String>(); 
		}
		urlList.add(url);
	}
	
	
	/**
	 * 加载url，每次调用新增一个url时，将url加载到urlNode树中
	 * @param url
	 */
	public void overlapNode(String url){
		addUrl(url);
		UrlNode node=getUrlAnalysis().analysisUrl(url);
		if(urlNode==null){
			urlNode=node;
		}else{
			getUrlAnalysis().overlapNode(urlNode, node);
		}
	}
	
	/**
	 * 批量加载urls
	 * @param urls
	 */
	public void loadUrls(List<String> urls){
		for(String url:urls){
			overlapNode(url);
		}
	}
	
	/**
	 * 清空
	 */
	public void reset(){
		setUrlNode(null);
		setUrlList(null);
	}
	
	private UrlAnalysis getUrlAnalysis(){
		return BeanFactory.createUrlAnalysis(UrlAnalysis.BEAN_NAME_URL_ANALYSIS);
	}
	
	

}
